import time
from selenium.webdriver.common.by import By
from pages.base_page import Page


class ShiftsPage(Page):
    ele_title = (By.XPATH, u"//h3[text()='Shifts']")
    ele_error = (By.XPATH, u'//div[@role="alert"]')

    # Not have this feature
    ele_unlock_button = (By.XPATH, u"//span[text()='Unlock This Feature']")
    ele_img = (By.XPATH, u'//img[@alt="Shifts Introduction"]')

    # Has this feature
    ele_feature_switch = (By.XPATH, u"//span[@data-tag='featureSwitch']")
    ele_enable_shifts = (By.XPATH, u"//span[@data-tag='featureSwitch']")

    # Create a new shift
    ele_new_shift_button = (By.XPATH, u"//span[text()='New Shift']")
    ele_add_name_text = (By.ID, u'form-shift-input-name')
    ele_select_time_zone_button = (By.XPATH, u"//div[@data-formfield='timeZone']")
    ele_time_zone = (By.XPATH, u"//li[@title='(UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi']")
    ele_delete_wednesday_working_hours_button = (By.XPATH, u"//tr[@id='drag-2']/td[2]/div/div/div[2]/span[1]")
    ele_add_friday_working_hours_button = (By.XPATH, u"//tr[@id='drag-4']/td[2]/div/div/div[2]/span[2]")
    ele_manage_member =(By.ID, u'alltoright')
    save_button = (By.XPATH, u"//span[text()='Save']")

    delete_shifts = (By.XPATH, u"//tr[@id='drag-0']/td[@data-tag='operation']/div/button[3]")
    confirm_delete_shifts =(By.XPATH, u"//span[text()='Delete']")

    def __init__(self, driver, url, site_id):
        Page.__init__(self, driver, url)
        self.site_id = site_id
        self.url = url

    def check_if_shifts_enable(self):
        fs = self.find_element(*self.ele_enable_shifts)
        if fs.text == 'OFF':
            self.click(self.ele_enable_shifts)
        else:
            pass
    def check_shift_page_when_has_shifts_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_enable_shifts).is_enabled()
        self.find_element(*self.ele_new_shift_button).is_enabled()

    def check_new_shift_when_has_shifts_feature(self):
        self.click(self.ele_new_shift_button)
        self.input_text(self.ele_add_name_text, 'autotest')
        self.click(self.ele_select_time_zone_button)
        self.click(self.ele_time_zone)
        self.click(self.ele_delete_wednesday_working_hours_button)
        # self.click(self.ele_add_friday_working_hours_button)
        self.click(self.ele_manage_member)
        time.sleep(3)
        self.click(self.save_button)

    def check_delete_shift_when_has_shifts_feature(self):
        self.click(self.delete_shifts)
        self.click(self.confirm_delete_shifts)

    def check_shift_page_when_no_shifts_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_unlock_button).is_enabled()
        self.find_element(*self.ele_img).is_enabled()


